// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_WORKGROUP
// * CreateDate    : 2014-02-18 18:04:09
// * Creater       : CodeSmith
// * LastChangeDate: 2014-02-18 18:04:09
// * LastChanger   : 2014-02-18 18:04:09
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Prod
{
/// <summary>
    /// ProdWorkgroup
    /// </summary>
    public partial class ProdWorkgroupBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdWorkgroupEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdWorkgroupEntity GetProdWorkgroup(ProdWorkgroupEntity objProdWorkgroupEntity)
        {
            return DataAccess.SelectSingle<ProdWorkgroupEntity>(objProdWorkgroupEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdWorkgroupEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdWorkgroup(ProdWorkgroupEntity objProdWorkgroupEntity)
        {
            return DataAccess.Insert(objProdWorkgroupEntity);
        }

        public static bool InsertProdWorkgroup(List<ProdWorkgroupEntity> objProdWorkgroupEntityList)
        {
            return DataAccess.Insert(objProdWorkgroupEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdWorkgroupEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdWorkgroup(ProdWorkgroupEntity objProdWorkgroupEntity)
        {
            return DataAccess.Update(objProdWorkgroupEntity);
        }

        public static bool UpdateProdWorkgroup(List<ProdWorkgroupEntity> objProdWorkgroupEntityList)
        {
            return DataAccess.Update<ProdWorkgroupEntity>(objProdWorkgroupEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdWorkgroupEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdWorkgroup(ProdWorkgroupEntity objProdWorkgroupEntity)
        {
            return DataAccess.Delete(objProdWorkgroupEntity);
        }

        public static bool DeleteProdWorkgroup(List<ProdWorkgroupEntity> objProdWorkgroupEntityList)
        {
            return DataAccess.Delete<ProdWorkgroupEntity>(objProdWorkgroupEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdWorkgroupAll()
        {
            return DataAccess.SelectAllDataSet<ProdWorkgroupEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdWorkgroupEntity> GetProdWorkgroupAllList()
        {
            return DataAccess.SelectAll<ProdWorkgroupEntity>();
        }

        public static List<ProdWorkgroupEntity> GetProdWorkgroupByCondition(ProdWorkgroupEntity objProdWorkgroupEntity)
        {
            if(objProdWorkgroupEntity==null)
                return DataAccess.SelectAll<ProdWorkgroupEntity>();
            else
                return DataAccess.Select(new ProdWorkgroupEntity { StationType = objProdWorkgroupEntity .StationType});
        }

      
        #endregion


        public static bool InsertProdWorkgroup(ProdWorkgroupEntity objProdWorkgroupEntity,List<ProdWorkgroupUserEntity> workgroupUsers)
        {

            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert(objProdWorkgroupEntity, broker);
                    if (workgroupUsers != null && workgroupUsers.Count > 0) DataAccess.Insert(workgroupUsers, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        public static bool UpdateProdWorkgroup(ProdWorkgroupEntity objProdWorkgroupEntity, List<ProdWorkgroupUserEntity> workgroupUsers)
        {

            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    List<ProdWorkgroupUserEntity> deleteWorkgroupUsers = DataAccess.Select<ProdWorkgroupUserEntity>(new ProdWorkgroupUserEntity { ProdWorkgroupId = objProdWorkgroupEntity.Id }).ToList();

                    broker.BeginTransaction();
                    DataAccess.Update(objProdWorkgroupEntity, broker);
                    if (deleteWorkgroupUsers != null && deleteWorkgroupUsers.Count > 0) DataAccess.Delete(deleteWorkgroupUsers);
                    if (workgroupUsers != null && workgroupUsers.Count > 0) DataAccess.Insert(workgroupUsers, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static List<ProdWorkgroupUserEntity> GetWorkGroupUsersByWorkGroupId(string workgroupId)
        {
            return DataAccess.Select<ProdWorkgroupUserEntity>(new ProdWorkgroupUserEntity { ProdWorkgroupId = workgroupId }).ToList();
        }
	}
	
	
}	

